package com.njlime.member.service.sys;

import com.njlime.cms.entities.MaterialsNews;
import com.njlime.core.helper.PageBean;
import com.njlime.member.entiies.MemberComment;
import com.njlime.member.entiies.TMember;
import com.njlime.member.repository.sms.MemberCommentDao;
import com.njlime.member.repository.sms.MemberDao;
import com.njlime.member.repository.sms.MemberGroupDao;
import com.njlime.sys.entities.Unit;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Dean on 2017-7-27.
 */
@Service
@Transactional(readOnly = true)
public class MemberService {

    @Resource
    private MemberDao memberDao;
    @Resource
    private MemberCommentDao memberCommentDao;

    @Resource
    private MemberGroupDao memberGroupDao;

    public PageBean list(PageBean pageBean, TMember member,String startDay,String endDate,String condition,String flag,String priceType,String orderType) {
        return memberDao.list(pageBean, member, startDay, endDate, condition, flag,priceType, orderType);
    }

    public PageBean getReportList(PageBean pb, String provinceId, String startDay, String endDay, String cityId) {
        return memberDao.getReportList(pb, provinceId, startDay, endDay, cityId);
    }

    public void addReportInfo(String startDate) {
        memberDao.addReportInfo(startDate);
    }

    public TMember findOne(String id) {
        return memberDao.findOne(Integer.valueOf(id));
    }

    /**
     * 显示 隐藏
     * @param info
     * @return
     */
    @Transactional(readOnly = false)
    public int updateStatus(TMember info) {
        return memberDao.updateStatus(  info);
    }

    @Transactional(readOnly = false)
    public void editMemberGroup(String ids, int groupId,int unitId) {
        if(ids.indexOf(",")>0){
            for(String id :ids.split(",")){

                TMember member =  memberDao.findOne(Integer.parseInt(id));
                // 减分组数量
                if(member.getMember_group_id() != null && member.getMember_group_id().intValue() >0){
                    memberGroupDao.downCount(member.getMember_group_id(),unitId);
                }
                if(groupId > 0) {
                    // 新增分组数量
                    memberGroupDao.upCount(groupId, unitId);
                }
                memberDao.editMemberGroup(Integer.parseInt(id), groupId, unitId);

            }
        }else {

            TMember member =  memberDao.findOne(Integer.parseInt(ids));
            // 减分组数量
            if(member.getMember_group_id() != null && member.getMember_group_id().intValue() >0){
                memberGroupDao.downCount(member.getMember_group_id(),unitId);
            }
            if(groupId > 0) {
                // 新增分组数量
                memberGroupDao.upCount(groupId, unitId);
            }
            memberDao.editMemberGroup(Integer.parseInt(ids), groupId, unitId);
        }

    }
    /**
     * 禁言
     * @param info
     * @return
     */
    @Transactional(readOnly = false)
    public int updateGag(TMember info) {

        return memberDao.updateGag(info);
    }

    public List<TMember> selectUserGroupList(int groupId, String ids,int unitId) {
        return memberDao.selectUserGroupList(groupId,ids,unitId);
    }

    /**
     * 用户评论
     * @param memberComment
     * @return
     */
    @Transactional
    public MemberComment saveComment(MemberComment memberComment){
        return memberCommentDao.save(memberComment);
    }


    public  List<TMember> getMemberBybirthList(Integer unitId, Date date) {
        return memberDao.getMemberBybirthList(unitId,date);
    }

    public List<Map<String,Object>> memberReport(Unit unit, Date start, Date end){
        return memberDao.memberReport(unit, start, end);
    }

    /**
     * 欢迎页面统计
     * @param unit
     * @return
     */
    public Map<String,Object> welcomeReport(Unit unit){
        return memberDao.welcomeReport(unit);
    }


    /**
     * 会员统计
     */
    @Transactional
    public void addReport() {
        memberDao.userReport();
    }


    /**
     * 统计
     * @return
     */
    public PageBean report(PageBean pageBean,Unit unit, Date startTime, Date endTime, Integer province_id, Integer city_id, boolean total){
        pageBean = memberDao.report(pageBean, unit, startTime, endTime, province_id, city_id);
        if(pageBean.getMapList() != null && pageBean.getMapList().size() > 0 && total){
            pageBean.getMapList().add(memberDao.sumReport(unit, startTime, endTime, province_id, city_id));
        }
        return pageBean;
    }

}
